import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../folding_cube.dart';

bool isShow = false;

String state = '1';

Future showLoginLoading(BuildContext context) {
  state = context.widget.runtimeType.toString();
  print(context.widget.runtimeType.toString());
  print('是否显示$isShow');
  if(isShow){
    return Future.value(null);
  }
  return showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        isShow = true;
        return new Material(
          color: Colors.transparent,
          //关闭对话框/*() => new Future.value(false)*/,
          child: Center(
            child: new Container(
              width: 200.0,
              padding: new EdgeInsets.all(4.0),
              decoration: new BoxDecoration(
                color: Colors.transparent,
                //用一个BoxDecoration装饰器提供背景图片
                borderRadius: BorderRadius.all(Radius.circular(4.0)),
              ),
              child: new Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
//                  Image.asset(R.assetsImagePubLoadingGif, width: px(400), height: px(600),),
                  SpinKitFoldingCube(),
                  new Container(height: 10.0),
                  new Container(child: new Text('加载中')),
                ],
              ),
            ),
          ),
        );
      }).then((_){
        isShow = false;
        state = '';
  });
}

void dismiss(BuildContext context) async{
  if(isShow){
    if(state != context.widget.runtimeType.toString()){return;}
    isShow = false;
    state = '';
    Navigator.of(context).pop(true);
    FocusScope.of(context).requestFocus(new FocusNode());
  }
}

class Loading extends StatelessWidget {
  final Widget child;
  final Widget loading;

  ///忽略loading下面的事件
  final bool ignoreTouch;

  const Loading({Key key, this.loading, this.child, this.ignoreTouch = true}) : super(key: key);

  @override
  Widget build(BuildContext context) {
//    var indicator = loading ?? Image.asset(R.assetsImagePubLoadingGif, width: px(400), height: px(600),);
    var indicator = loading ?? SpinKitFoldingCube();
    return Stack(
      children: <Widget>[
        (child != null) ? Positioned.fill(child: child) : Container(),
        (!ignoreTouch)?
          Center(
            child: indicator,
          ) : Container(),
        (ignoreTouch)?
          Positioned.fill(
            child: GestureDetector(
              behavior: HitTestBehavior.opaque,
              child: Container(
                child: Center(
                  child: indicator,
                ),
              ),
            ),
          ) : Container(),
      ],
    );
  }
}

class Error extends StatelessWidget {
  final Widget child;
  final Widget loading;

  ///忽略Error下面的事件
  final bool ignoreTouch;

  const Error({Key key, this.loading, this.child, this.ignoreTouch = true}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var indicator = loading ?? Icon(Icons.sms_failed,size: 200,);
    return Stack(
      children: <Widget>[
        (child != null) ? Positioned.fill(child: child) : Container(),
        (!ignoreTouch) ?
          Center(
            child: indicator,
          ) : Container(),
        (ignoreTouch) ?
          Positioned.fill(
            child: GestureDetector(
              behavior: HitTestBehavior.opaque,
              child: Container(
                child: Center(
                  child: indicator,
                ),
              ),
            ),
          ) : Container(),
      ],
    );
  }
}

class Empty extends StatelessWidget {
  final Widget child;
  final Widget loading;

  ///忽略Empty下面的事件
  final bool ignoreTouch;

  const Empty({Key key, this.loading, this.child, this.ignoreTouch = true}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var indicator = loading ?? Icon(Icons.hourglass_empty,size:100,);
    return Stack(
      children: <Widget>[
        (child != null) ? Positioned.fill(child: child) : Container(),
         (!ignoreTouch)?
          Center(
            child: indicator,
          ) : Container(),
        (ignoreTouch)?
          Positioned.fill(
            child: GestureDetector(
              behavior: HitTestBehavior.opaque,
              child: Container(
                child: Center(
                  child: indicator,
                ),
              ),
            ),
          ) : Container(),
      ],
    );
  }
}
